package com.project.controller;

//导入挂号接口
import com.Entity.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.project.util.AddCookies;
import org.apache.commons.codec.language.bm.Lang;
import org.springframework.web.bind.annotation.*;
import acl.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.util.*;

/**
 * 挂号controller
 */
@RestController
@RequestMapping("/acl")
public class RegisterController {

    //挂号服务接口
    @Reference
    private RegisterService registerService;

    @Resource
    private AddCookies addCookies;

    Map<String,Object> map ;

    @RequestMapping("/getHospitalInfo")
    /**
     * Description: 查询出所有医院<br/>
     * @author: lenovo<br/>
     * @date: 2018/12/27 14:28<br/>
     * @param:[]<br/>
     * @return:java.util.List<com.Entity.DzmHisHospitalEntity>
     */
    public List<DzmHisHospitalEntity>  getHospitalInfo(){
        return registerService.getHospital();
    }


    @RequestMapping("/getDepartment")
    /**
     * Description: 获取医院的所有科室<br/>
     * @author: lenovo<br/>
     * @date: 2018/12/27 14:29<br/>
     * @param:[hid : 医院id, response]<br/>
     * @return:java.util.List<com.Entity.DzmHisDepartmentEntity>
     */
    public  List<DzmHisDepartmentEntity> getFaculty(Integer hid, HttpServletResponse response){
        System.out.println(hid+"  :hid");
        //将医院id放入cookie
        Cookie cookie  =  new Cookie("hid",hid+"");
        response.addCookie(cookie);
        return  registerService.getFaculty(hid);
    }

    @RequestMapping("/getClinic")
    /**
     * Description: 获取所有门诊<br/>
     * @author: lenovo<br/>
     * @date: 2018/12/27 11:29<br/>
     * @param:[]<br/>
     * @return:java.util.List<com.Entity.DzmHisClinicEntity>
     */
    public List<DzmHisClinicEntity> getClinic(){
        return  registerService.getClinics();
    }

    @RequestMapping("/getSpecialists")
    /**
     * Description: 获取该医院下  该科室的所有医生<br/>
     * @author: lenovo<br/>
     * @date: 2018/12/27 10:36<br/>
     * @param:[hid : 医院id, did ： 科室id, cid ： 诊所id, response, request]<br/>
     * @return:java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> getSpecialists(@CookieValue("hid")String hid, Integer did, Integer cid, HttpServletResponse response, HttpServletRequest request){
        map = new HashMap<String,Object>();
        map.put("did",did);
        map.put("cid",cid);
        System.out.println(cid+"    "+did+"jjjjjjjjjjjjjjjjjjjjjjjj");
        addCookies.setCookie(response,map,request);//将数据放入cookie
        return  registerService.gethospitalDoctors(hid,did+"");
    }

    /**
     * Description: 查询出医生的详细信息 以及排班<br/>
     * @author: lenovo<br/>
     * @date: 2018/12/27 14:32<br/>
     * @param:[doctor_id: 选择的医生id]<br/>
     * @return:
     */
    @RequestMapping("/getDoctorInfo")
    public Map<String,Object> getDoctorInfo (Integer doctor_id,HttpServletRequest request,  HttpServletResponse response){
        map = new HashMap<>();
        map.put("doctorInfo",registerService.getDoctorInfo(doctor_id));
        Map<String,String> params = addCookies.getCookie(request);
        params.put("doctor_id",doctor_id+"");
        map.put("schedulingInfo",registerService.getSchedulingInfo(params));
        response.addCookie(new Cookie("doctor_id",doctor_id+""));
        return map;
    }
    /**
     * Description: 一个简单的获取所有cookie 的方法<br/>
     * @author: lenovo<br/>
     * @date: 2018/12/28 20:08<br/>
     * @param:<br/>
     * @return:
     */
    @RequestMapping("/getCookie")
    public Map<String,Object> getCookie(HttpServletRequest request){
        map = new HashMap<>();
        Cookie [] cookies = request.getCookies();
        for(Cookie cookie: cookies){
            map.put(cookie.getName(),cookie.getValue());
        }
        return map;
    }

    /**
     * Description: 获取指定医生指定日期的排班<br/>
     * @author: lenovo<br/>
     * @date: 2019/1/2 20:41<br/>
     * @param:<br/>
     * @return:
     */
    @RequestMapping("/getScheduling")
    public List<Map<String,Object>>  getScheduling(HttpServletRequest request,String date){
        Map<String,String> map = addCookies.getCookie(request);
        map.put("date",date);
        return  registerService.getScheduling(map);
    }

    /**
     * Description: 获取指定日期的所有医生的排班<br/>
     * @author: lenovo<br/>
     * @date: 2019/1/3 8:06<br/>
     * @param:<br/>
     * @return:
     */
    @RequestMapping("/getAllNowDector")
    public List<Map<String,Object>> getAllNowDector(HttpServletRequest request,String date){
        Map<String,String> map = addCookies.getCookie(request);
        System.out.println(date);
        if (date == null || date .equals("[object MouseEvent]")){
            DateFormat df1 = DateFormat.getDateInstance();
            date = df1.format(new Date());
            map.put("date",date);
        }else{
            long datetime = new Date().getTime();
            datetime += 24 * 60 * 60 * 1000 * Integer.parseInt(date);
            DateFormat df1 = DateFormat.getDateInstance();
            date = df1.format(new Date(datetime));
            map.put("date",date);
        }
        return  registerService.getAllNowDector(map);

    }

    /**
     * Description: 获取已经选择的数据<br/>
     * @author: lenovo<br/>
     * @date: 2019/1/3 8:26<br/>
     * @param:scheduling_week_id [排班id]<br/>
     * @return:
     */
    @RequestMapping("/getChooseInfo")
    public Map<String,Object> getChooseInfo(HttpServletRequest request,String scheduling_week_id){
        //删除所有cookie  防止冲突  影响性能
        addCookies.remove(request);
        return  registerService.getChooseInfo(scheduling_week_id);
    }

    /**
     * Description: 生成订单<br/>
     * @author: lenovo<br/>
     * @date: 2019/1/3 17:34<br/>
     * @param:<br/>
     * @return:
     */
    @RequestMapping("/confirmPay")
    public String confirmPay(@RequestParam String scheduling_week_id,HttpServletResponse response){
        String from = registerService.orderGeneration(scheduling_week_id);
        /*try {
            response.getWriter().write(from);
            response.getWriter().flush();
            response.getWriter().close();
        }catch (IOException e){
            e.printStackTrace();
        }*/

        return from;
    }

    @RequestMapping("/registerPay")
    public void registerPay(HttpServletRequest request,HttpServletResponse response){
        System.out.println("Ali 支付回调成功");
    }
}
